package com.android.likeweixin.util;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class Util {
	public static void hideInputMethod(Activity a) {
		InputMethodManager imm = (InputMethodManager) a
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null) {
			View focus = a.getCurrentFocus();
			System.out.println("##focus:" + focus);
			if (focus != null) {
				IBinder binder = focus.getWindowToken();
				if (binder != null) {
					if (imm.isActive()) {
						System.out.println("##focus11:" + focus);
						imm.hideSoftInputFromWindow(binder,
								InputMethodManager.HIDE_IMPLICIT_ONLY);
					}
				}
			}
		}
	}

	public static void hideInputMethod(Context a, EditText et) {
		InputMethodManager imm = (InputMethodManager) a
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
	}

	public static void showInputMethod(Activity a, EditText et) {
		InputMethodManager imm = (InputMethodManager) a
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null) {
			imm.showSoftInput(et, 0);
		}
	}

	public static String makeUpdateTime(long newsTime) {
		long nowTime = System.currentTimeMillis();
		long delta = (nowTime - newsTime) / 1000;
		if (delta < 60) {
			return delta + "秒前";
		}
		delta = delta / 60;
		if (delta < 60) {
			return delta + "分前";
		}
		delta = delta / 60;
		if (delta < 24) {
			return delta + "小时前";
		}
		delta = delta / 24;
		if (delta < 10) {
			return delta + "天前";
		}
		//显示日期,超过10天
		return DateTimeUtils.dateToString(new Date(newsTime), "yyyy-MM-dd HH:mm");
	}
}
